<?php
namespace app\model\frontend;
use think\Model;
use app\model\frontend\GoodsModel;
use app\model\frontend\PaymentModel;
use app\model\frontend\ShippingModel;
use app\model\frontend\AddressModel;
use app\model\tool\ImageModel;
// use sm_shop_plugin_distribution\model\yiyangCardModel;
// use sm_shop_plugin_distribution\model\yiyangAgentModel;
use app\model\frontend\YiyangCardModel;
use app\model\frontend\YiyangAgentModel;

class OrderModel extends model{

    protected $table = 'sh_order';

    public static function create2( $data ){

        global $_W;
        $total = $data['total']['total']['price'];
        $create_time = date("Y-m-d H:i:s");
        $sql = "INSERT INTO sh_order SET ".
            "`order_no`='" . $data['order_no'] . "',".
            "`uniacid`='" . $_W['uniacid'] . "',".
            "`create_time`='" . $create_time . "',".
            "`customer_id`='" . $data['customer_id'] . "',".
            "`total`='" . $total . "',".
            "`shipping_method`='" . $data['shipping_method'] . "',".
            "`payment_method`='" . $data['payment_method'] . "',".
            "`is_payed`='" . $data['is_payed'] . "',".
            "`address_name`='" . $data['address_name'] . "',".
            "`address_phone`='" . $data['address_phone'] . "',".
            "`address_province`='" . $data['address_province'] . "',".
            "`address_city`='" . $data['address_city'] . "',".
            "`address_area`='" . $data['address_area'] . "',".
            "`address_detail`='" . $data['address_detail'] . "',".
            "`message`='" . $data['message'] . "',".
            "`status`='" . $data['status'] . "';";

//        $res = self::$db->query( $sql );
        $res_num_rows = pdo_query( $sql );
        $order_id = 0;
        if( $res_num_rows ){
            
            $order_id = pdo_insertid('sh_order');

            //todo 添加 订单商品表
            self::add_order_goods( $order_id, $data['goods'] );

            //todo 添加 订单总计表
            self::add_order_total( $order_id, $data['total'] );

            //todo 添加 订单自提表
            if( $data['shipping_method'] == 'pickup' && !empty($data['pickup']) ){
                self::add_order_pickup( $order_id, $data['pickup'] );
            }

            //todo 添加 拼单信息
            if( !empty($data['is_pindan'])  ){

                self::add_order_pindan( $order_id, $data['pindan_order'] );

                // 如果为发起的拼单，添加拼单属性
                if( empty( $data['pindan_order'] ) ){
                    $goods_id = current($data['goods'])['goods_id'];

                    self::add_order_pindan_info( $order_id, $goods_id );
                }
            }

            // todo 添加优惠券记录
            if( isset($data['coupon']) ){
                self::add_order_coupon( $order_id, $data['coupon'] );
            }

            //todo 添加订单历史
            $history_data = [
                'comment'=>$data['message'],
                'order_status'=>$data['status'],
            ];
            self::add_order_history( $order_id, $history_data );

            // todo 添加分销记录，如果分销模块存在的话
            self::distribution( $order_id );


        }


        return $order_id;

    }

    public static function get_order_pickup( $order_id ){

        $sql = "select * from sh_order_pickup where order_id=" . $order_id ;

        return pdo_fetch( $sql );

    }

    public static function add_order_pickup( $order_id, $pickup ){

        global $_W;
        $sql = 'insert into sh_order_pickup set '
            . ' order_id=' . $order_id . ','
            . ' uniacid=' . $_W['uniacid'] . ','
            . ' pickup_id=' . $pickup['id'] . ','
            . ' pickup_name="' . $pickup['name'] . '",'
            . ' pickup_phone="' . $pickup['phone'] . '"'
        ;
        pdo_query( $sql );
    }

    public static function add_order_goods( $order_id, $order_goods ){

        global $_W;
        $sql = "insert into sh_order_goods (order_id,uniacid,goods_id,goods_name,goods_option,goods_num,goods_price,goods_total) values";
        $sql_split = '';
        foreach( $order_goods as $goods ){

            $sql_value = "('" . $order_id ."','"
                . $_W['uniacid'] ."','"
                . $goods['goods_id'] ."','"
                . $goods['name'] ."','"
                . $goods['goods_option'] ."','"
                . $goods['goods_num'] ."','"
                . $goods['price'] ."','"
                . $goods['total_price'] ."')";
//            $sql_data += "order_id='" . $order_id ."',";
//            $sql_data += "goods_id='" . $goods['goods_id'] ."',";
//            $sql_data += "goods_name='" . $goods['name'] ."',";
//            $sql_data += "goods_num='" . $goods['goods_num'] ."',";
//            $sql_data += "goods_price='" . $goods['price'] ."',";
//            $sql_data += "goods_total='" . $goods['total'] ."',";

            if( !empty($sql_split) ){
                $sql_split .= ',';
            }
            $sql_split .= $sql_value;


        }
        $sql .= $sql_split;

        pdo_query( $sql );
    }

    public static function add_order_total ( $order_id, $order_total ){
        global $_W;
        $sql = "insert into sh_order_total ( order_id, uniacid, code, `value` ) values";
        $sql_split = '';

        foreach( $order_total as $key=>$total ){

            $sql_value = "('" . $order_id ."','"
                .  $_W['uniacid'] ."','"
                .  $key ."','"
                . $total['price'] ."')";

            if( !empty($sql_split) ){
                $sql_split .= ',';
            }
            $sql_split .= $sql_value;
        }
        $sql .= $sql_split;

        pdo_query( $sql );
    }


    public static function get_list( $customer_id, $filter, $page, $page_size ){

        $sql = 'SELECT o.*, os.`name` AS status_text FROM sh_order o '
            . ' LEFT JOIN sh_order_status os ON o.`status`=os.code ';

        $sql_page = ' limit ' . ($page - 1) * $page_size .','. $page_size;

        $where = ' where o.`status`<>"canceled" and o.customer_id='. $customer_id ;
        if( !empty($filter) ){
            foreach( $filter as $key => $value ){
                $where .= ' and o.' . $key . '= "' . $value . '" ';
            }
        }

        $sort_order = ' order by create_time desc ';

        $sql .= $where . $sort_order . $sql_page ;
//        $res = self::$db->query( $sql );
//        return $res->num_rows ? $res->rows: [];

        return pdo_fetchall( $sql );
    }

    public static function get_single( $order_id ){

        $sql = "select * from sh_order where id=" . $order_id;
        return pdo_fetch( $sql );

    }

    public static function get_order_number( $customer_id, $filter=array() ){

        $sql = 'select count(id) as total from sh_order o ';

        $where = ' where o.`status`<>"canceled" and o.customer_id='. $customer_id ;
        if( !empty($filter) ){
            foreach( $filter as $key => $value ){
                $where .= ' and o.' . $key . '= "' . $value . '" ';
            }
        }
        $sql .= $where ;
        $res = pdo_fetchcolumn( $sql );
        return $res;
    }

    public static function get_order_goods( $order_id ){

        $sql = 'SELECT og.*,g.image FROM sh_order_goods og'
            . ' inner join sh_goods g on og.goods_id=g.id '
            . ' WHERE order_id=' . $order_id;
//        $res = self::$db->query( $sql );
//        return $res->num_rows ? $res->rows : [];

        return pdo_fetchall( $sql );
    }

    public static function get_order_total( $order_id ){

        $sql = 'SELECT * FROM sh_order_total WHERE order_id=' . $order_id;
//        $res = self::$db->query( $sql );
//        return $res->rows;

        return pdo_fetchall( $sql );
    }

    public static function findByNo( $customer_id, $order_no ){

        $sql = 'select o.*, p.name as payment_name, s.name as shipping_name from sh_order o '
            . ' left join sh_payment p on o.payment_method=p.code '
            . ' left join sh_shipping s on o.shipping_method=s.code '
            . ' where o.customer_id=' . $customer_id
            . ' and o.order_no="' . $order_no .'"';

        return pdo_fetch( $sql );
    }

    public static function findById( $customer_id, $order_id ){

        $sql = 'select o.*, p.name as payment_name, s.name as shipping_name from sh_order o '
            . ' left join sh_payment p on o.payment_method=p.code '
            . ' left join sh_shipping s on o.shipping_method=s.code '
            . ' where o.customer_id=' . $customer_id
            . ' and o.id=' . $order_id;
//        $res = self::$db->query( $sql );
//        return $res->num_rows ? $res->row : null;

        return pdo_fetch( $sql );
    }

    public static function cancel( $customer_id, $order_id ){

        $sql = 'update sh_order set `status`="canceled" where customer_id=' . $customer_id . ' and id=' . $order_id;
//        $res = self::$db->query( $sql );
//        return $res->num_rows > 0 ;

        $res = pdo_query( $sql );

        // todo 取消订单
        $log_data = [
            'order_status'=>'canceled',
            'comment'=>'用户取消订单',
        ];
        self::add_order_history($order_id, $log_data );

        // todo 查看优惠券， 取消订单 恢复优惠券（待定，或许不恢复）
        return $res > 0;
    }

    public static function get_history_list( $order_id ){

        $sql = 'SELECT 
              h.*,
              s.`name` as order_status_text
            FROM
              sh_order_history h 
              LEFT JOIN sh_order_status s 
                ON h.`order_status` = s.`code` where h.order_id=' . $order_id . ' order by h.create_time desc';

//        $query = self::$db->query( $sql );
//
//        return $query->rows;

        return pdo_fetchall( $sql );

    }

    public static function add_order_pindan_info( $order_id, $goods_id ){

        global $_W;
        $sql = "select * from sh_goods_pindan where goods_id=" . $goods_id;

        $goods_pindan = pdo_fetch( $sql );

        if( !empty( $goods_pindan ) ){

            $date = date('Y-m-d H:i:s');
            $insert_sql = 'insert into sh_order_pindan_info set order_id=' . $order_id
            . ', goods_id=' . $goods_id
            . ', uniacid=' . $_W['uniacid']
            . ', number=' . $goods_pindan['number']
            . ', validate_time="' . $goods_pindan['validate_time'] . '"'
            . ', complete=0'
            . ', start_time="' . $date . '"'
            ;
            pdo_query( $insert_sql );
        }


    }

    public static function add_order_pindan( $order_id, $pindan_order ){

        global $_W;
        $pindan_order = !empty( $pindan_order ) ? $pindan_order : $order_id;
        $sql = 'insert into sh_order_pindan set order_id= ' . $order_id
            . ', uniacid="' . $_W['uniacid'] . '"'
            . ', master_order="' . $pindan_order . '"';

        pdo_query( $sql );

    }

    public static function pindan_list( $goods_id ){

        $sql = 'select opi.order_id, opi.start_time, opi.validate_time, c.name, c.headUrl '
            . ' from sh_order_pindan_info opi'
            . ' left join sh_order o on opi.order_id=o.id'
            . ' left join sh_customer c on o.customer_id=c.id'
            . ' where goods_id=' . $goods_id
        . ' and DATE_SUB(NOW(),INTERVAL validate_time HOUR )<start_time';


        return pdo_fetchall( $sql );
    }


    public static function add_order_coupon( $order_id, $coupon ){

        global $_W;
        $sql = 'insert into sh_order_coupon set order_id= ' . $order_id
            . ', uniacid="' . $_W['uniacid'] . '"'
            . ', coupon_id="' . $coupon['id'] . '"'
            . ', coupon_amount="' . $coupon['amount'] . '"'
        ;

        if( pdo_query( $sql ) ){
            couponModel::consume($coupon['id']);
        }

    }

    public static function add_order_history( $order_id, $data ){

        global $_W;
        $create_time = date("Y-m-d H:i:s");
        $sql = 'insert into sh_order_history set order_id= ' . $order_id
            . ', uniacid="' . $_W['uniacid'] . '"'
            . ', order_status="' . $data['order_status'] . '"'
            . ', comment="' . $data['comment'] . '"'
            . ', create_time="' . $create_time . '"'
        ;
        pdo_query( $sql );
    }

    public static function order_pay( $order_id, $order_status,$payment='wechat' ){

        $sql = "update sh_order set is_payed = 1 "
            . ", status='" . $order_status
            . "', payment_method='" . $payment
            . "' where id=" . $order_id;
        $history_data = [
            'comment'=>'订单支付',
            'order_status' => $order_status,
        ];

        if( $ret = pdo_query( $sql ) ){
            self::add_order_history( $order_id, $history_data );

            // todo 是否添加 优惠券
//          mc_credit_update($uid, $credittype, $creditval = 0 );

            // todo 添加分销订单的处理
//          self::activeOrderDistribution( $order_id );
            self::activeOrderDistribution2( $order_id );
//          return pdo_query( $sql );
        }

        return $ret;

    }


    /**
     * @param $checkout
     * @return mixed
     * 计算订单费用， 地址， 付款形式等
     */
    public static function calculate( &$checkout ){

        global $_W, $_GPC;
        // $customer_id = $_W['member']['uid']; // 独立版没有这个参数
        $customer_id = $_W['customer']['id'];
        $total = 0;

        $goods_total = 0;

        foreach( $checkout['goods'] as $key=>&$item ){

            $goods = goodsModel::detail( $item['goods_id'] );

            $goods_total += $item['price'] * $item['goods_num'];

            $checkout['goods'][$key] = [
                'goods_id' => $item['goods_id'],
                'goods_num' => $item['goods_num'],
                'name' => $item['name'],
                'goods_option' => $item['goods_option'],
                'price' => $item['price'],
                'total_price'=> round(  floatval( $item['price']) * intval( $item['goods_num'] ),2),
                'img' => imageModel::resize2( $goods['image'], 100, 100 ),
            ];
        }

        $checkout['total']['goods'] = [
            'name'=>'商品总额',
            'price'=>round( $goods_total, 2)
        ];
        $total += $goods_total;

        if( isset($checkout['coupon']) ){
            $coupon_total = - round( $checkout['coupon']['amount'],2 );
            $checkout['total']['coupon'] = [
                'name'=>'优惠券',
                'price'=> $coupon_total
            ];
            $total += $coupon_total;
        }

        $shipping_total = 0;
        $checkout['total']['shipping'] = [
            'name'=>'运费',
            'price'=> $shipping_total
        ];
        $total += $shipping_total;

        $total = $total>0?$total:0;
        $checkout['total']['total'] = [
            'name'=>'订单总额',
            'price'=>round($total,2)
        ];



        if( empty( $checkout['payment_code']) ){


            $default_payment = paymentModel::getDefault();

            $checkout['payment_code'] = $default_payment['code'];

        }

        if( empty( $checkout['shipping_code']) ){

            $default_shipping = shippingModel::getDefault();
            $checkout['shipping_code'] = $default_shipping['code'];

        }

        if( empty( $checkout['address_id']) ) {

            $address = addressModel::getDefault( $customer_id );
            $checkout['address_id'] = !empty($address)? $address['id']:'';
        }

    }

    public static function get_order_package( $order_id ){


        $sql = "select op.*,e.name from sh_order_package op left join sh_express e on op.express_code=e.code where op.order_id= " . $order_id;

        return pdo_fetchall( $sql );

    }

    public static function applyForRefund( $customer_id, $order_id ){


        $sql = 'update sh_order set `apply_refund`=1 where customer_id=' . $customer_id . ' and id=' . $order_id;
        $ret = pdo_query( $sql );

        $order = self::findById($customer_id, $order_id );

        // todo 添加订单历史备注，暂不改变状态
        $log_data = [
            'order_status'=> $order['status'],
            'comment'=>'用户申请退款',
        ];

        self::add_order_history($order_id, $log_data );

        return $ret;

    }


    /**
     * 订单分销
     */
    public static function distribution( $order_id ){

        global $_W;

        // 查看是否安装分销插件
        $plugin_list = $_W['current_module']['plugin_list']??[];

        if( in_array( 'sm_shop_plugin_distribution', $plugin_list )
            && file_exists(IA_ROOT . '/addons/sm_shop_plugin_distribution/model/distributorModel.php')
        ){
            \sm_shop_plugin_distribution\model\distributorModel::setOrderDistribution( $order_id );
        }

    }

    /**
     * 分销订单变为可提现状态
     */
    public static function activeOrderDistribution( $order_id ){

        global $_W;
        // 查看是否安装分销插件
        $plugin_list = $_W['current_module']['plugin_list'];
        if( in_array( 'sm_shop_plugin_distribution', $plugin_list )
            && file_exists(IA_ROOT . '/addons/sm_shop_plugin_distribution/model/distributorModel.php')
        ){
            \sm_shop_plugin_distribution\model\distributorModel::activeOrderDistribution( $order_id );
        }
    }

    // 分销处理
    public static function activeOrderDistribution2( $order_id ){

        $fenxiao_mode = settingModel::getItem('distribution','mode');

        if( !$fenxiao_mode ){
            return;
        }
        $order = self::get_single( $order_id );

        // 基础分销
        if( $fenxiao_mode == 'distribution' ){


        }

        // 医养分销
        if( $fenxiao_mode == 'distribution_yiyang' ){
            $level = settingModel::getItem('distribution_yiyang','level');
            $level = json_decode( $level, true );
            $xiaofeizhe_amount = floatval( $level['xiaofeizhe']['amount'] );
            $hehuoren_amount = floatval( $level['hehuoren']['amount'] );
            $total = floatval( $order['total'] );
            if( $total>= $hehuoren_amount ){

                // todo 添加用户到合伙人级别信息， 并生成相应数目的优惠券
                $cardData = [
                    'card_type'=> 'hehuoren',
                    'coupon_amount'=> $level['hehuoren']['quan_amount'],
                    'coupon_number'=> $level['hehuoren']['quan_number'],
                ];
                yiyangCardModel::add( $order['customer_id'], $cardData);

            }else if( $total >= $xiaofeizhe_amount ){
                // todo 添加用户到消费者级别信息， 并生成相应数目的优惠券
                $cardData = [
                    'card_type'=> 'xiaofeizhe',
                    'coupon_amount'=> $level['xiaofeizhe']['quan_amount'],
                    'coupon_number'=> $level['xiaofeizhe']['quan_number'],
                ];
                yiyangCardModel::add( $order['customer_id'], $cardData);
            }

            //todo 医养分销代理分红添加
            //todo 医养分销代理升级检测
            yiyangAgentModel::orderCheck( $order );

        }
    }

    /**
     * 订单确认收货
     */
    public function receive( $customer_id, $order_id ){

//        $sql = 'update sh_order set `status`="apprising" where customer_id=' . $customer_id
//            . ' and id=' . $order_id;

//        $res = pdo_query( $sql );
        $data = [
            'status'=>'apprising'
        ];
        $where = [
            'customer_id'   => $customer_id,
            'id'            => $order_id,
        ];
        $res = sm_pdo_update('order', $data, $where );

        // todo 取消订单
        $log_data = [
            'order_status'=>'apprising',
            'comment'=>'用户确认收货',
        ];
        self::add_order_history($order_id, $log_data );

        return $res > 0;

    }
}